package onlinecalsys.server.controller;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.List;

import onlinecalsys.client.controller.CalsysClientController;
import onlinecalsys.client.controller.ClientController;
import onlinecalsys.client.view.ObserverCommon.Observer;
import onlinecalsys.server.model.CalsysModel;
import onlinecalsys.server.model.ServerModel;
import onlinecalsys.server.objects.Appointment;

public class CalsysServerController extends UnicastRemoteObject implements
		ServerController {

	private ServerModel model;
	private List<Observer> observerList;

	public CalsysServerController() throws RemoteException {
		super();
		model = new CalsysModel(this);
		observerList = new ArrayList<Observer>();

	}

	@Override
	public boolean addApointment(Appointment app) throws RemoteException {
		boolean status = model.addAppointment(app);
		System.out.println("servercontroller returning " + status);
		return status;
	}

	@Override
	public ArrayList<ArrayList<String>> getAppointments(String date,
			String username) throws RemoteException {
		return model.getAppointments(date, username);
	}

	@Override
	public boolean deleteAppointment(int appointmentId, String username)
			throws RemoteException {
		return model.deleteAppointment(appointmentId, username);

	}

	@Override
	public boolean usernameChecking(String username, String password)
			throws RemoteException {
		return model.usernameChecking(username, password);
	}

	@Override
	public boolean saveNote(String note,String username) throws RemoteException {

		return model.saveNote(note,username);
	}
	
	@Override
	public String getNote(String username) throws RemoteException {
		
		return  model.getNote(username);
	}
	

	@Override
	public boolean shareAppointment(String username, int id,
			String shareWithUser) throws RemoteException {

		return model.shareAppointment(username, id, shareWithUser);
	}

	@Override
	public void attach(Observer client) throws RemoteException {
		System.out.println("adding client");
		observerList.add(client);
	}

	@Override
	public void dettach(String username) throws RemoteException {
		System.out.println("detaching: " + username);
		ClientController clientController = null;
		for(ClientController c : observerList) {
			if(username.equals(c.getUsername())) {
				clientController = c;
			}
		}
		if(clientController != null) {
			observerList.remove(clientController);
		}
	}

	@Override
	public void notifyObservers(String username) throws RemoteException {
		System.out.println("notifying");
		for (Observer o : observerList) {
			try {
				o.update(username);
			} catch (RemoteException e) {
				e.toString();
				System.out.println("trying to dettach: " + username);
				dettach(username);
			}
		}
	}

	@Override
	public ArrayList<ArrayList<String>> getSharedAppointments(String username)
			throws RemoteException {
		return model.getSharedAppointments(username);

	}

	@Override
	public boolean createNewUser(String username, String password)
			throws RemoteException {
		return model.createNewUser(username, password);
	}

	

}
